
//
//  ResetPasswordViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/9.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import DynamicBlurView
import Alamofire
import PKHUD
import YYModel
import SwiftyJSON

class ResetPasswordViewController: BaseViewController {
    @IBOutlet weak var bgImageView: UIImageView!
    @IBOutlet weak var showPWView: UIView!
    @IBOutlet weak var showPwTextField: UITextField!
    
    var phone:String?
    var vcode: String?
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        titleLabel(text: "重置密码", nil)
        let blurView = DynamicBlurView(frame: bgImageView.bounds)
        blurView.blurRadius = 10
        view.insertSubview(blurView, aboveSubview: bgImageView)
        
        showPwTextField.attributedPlaceholder = NSAttributedString(string: "输入至少8位数密码", attributes: [NSAttributedStringKey.foregroundColor : UIColor(hex: 0xB6B6B6)])
        
        showPWView.layer.borderColor = UIColor.white.cgColor
        showPWView.layer.borderWidth = 1
    }
    
    override func setNavigation() {
    }
    
    @IBAction func showPwTap(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        showPwTextField.isSecureTextEntry = sender.isSelected
    }
    
    @IBAction func doneTap(_ sender: UIButton) {
        
        if (showPwTextField.text?.isEmpty)! {
            self.showToast(msg:"请输入新密码")
            return
        }
        if !(self.isValidPwd(pwd: showPwTextField.text! as NSString)) {
            self.showToast(msg:"请输入至少8位数密码")
            return
        }
        let requesturl = "\(BaseURL)\(resetPwdUrl)"
        let parameter:[String:Any] = ["mp":phone!,"newpsw":showPwTextField.text!,"vcode":
        vcode!]
        Alamofire.request(requesturl, method: .post,
                          parameters: parameter,
                          encoding: URLEncoding.default,
                          headers: nil).responseJSON {
                            (response) in
                            HUD.hide()
                            if(response.error == nil){
                                self.showToast(msg:"重置成功")
                                self.navigationController?.popToRootViewController(animated: true)
                                
                            }else{
                                print("请求失败\(String(describing: response.error))")
                            }
                            
        }
        HUD.show(.label("加载中..."))

        
        
    }
}
